<?php

namespace app\modules\api\controllers;

use app\models\ApiException;
use app\models\Agent;
use app\models\Card;
use app\models\MembershipKey;
use Yii;
class MembershipController extends BasePublicController{

        public function actionIndex(){
                $iccid = $this->get('iccid');
                $iccidLength = strlen($iccid);
                if($iccidLength>19){
                    $iccid = substr($iccid,0,strlen($iccid)-1);
                }
                $card = Card::find()->andFilterWhere(['like','iccid',$iccid])->one();
                $memship = MembershipKey::find()->andWhere(['status'=>0])->all();
                if(empty($memship)){
                    $data['code'] = '1';
                    $data['msg'] = '卡密没有剩余，请等待！';
                    echo json_encode($data);
                    exit;
                }
                if(!empty($card->active_time)){
                        $iccids = MembershipKey::find()->andWhere(['iccid'=>$card->iccid])->one();
                        if(empty($iccids)){
                            $key = MembershipKey::find()->andWhere(['status'=>0])->one();
                            $member = MembershipKey::findOne(['id'=>$key->id]);
                            Yii::$app->db->createCommand()->update('membership_key', ['status' => 1,'iccid'=>$card->iccid,'execute_time'=>time()], 'id = :id',['id' => $member->id])->execute(); 
                            $data['code'] = '1';
                            $data['iccid'] = $card->iccid;
                            $data['msg'] = '获取卡密成功';
                            $data['key'] = $key->secret_key;
                            echo json_encode($data);
                            exit;     
                        }else{
                            $keys = MembershipKey::find()->andWhere(['status'=>1])->andWhere(['iccid'=>$card->iccid])->one();
                            $data['code'] = '1001';
                            $data['msg'] = '该卡已经领取，卡密为：'.$keys->secret_key;
                            echo json_encode($data);
                            exit;  
                        }
                        
                }else{
                        $data['code'] = '1001';
                        $data['msg'] = '卡没有充值，请充值以后再进行领取！';
                        echo json_encode($data);
                        exit;
                }

               
        }

        public function microtime(){
            $t = explode(" ", microtime());
            $ts = round(round($t[1].substr($t[0],2,3)));
            return $ts;
        }
        /* 毫秒时间戳转换成日期 */
        public function msecdate($time)
        {
            $tag='Y-m-d H:i:s';
            $a = substr($time,0,10);
            $b = substr($time,10);
            $date = date($tag,$a).' '.$b;
            return $date;
        }
        /*
         * tran_id 处理
         */
        public function msecdate2($time)
        {
            $tag='YmdHis';
            $a = substr($time,0,10);
            $b = substr($time,10);
            $date = date($tag,$a).$b.rand('100000','999999');
            return $date;
        }
        public function actionRealNameStatusQuerymy(){
        $iccid = $this->get('iccid');
        $iccidLength = strlen($iccid);
        if($iccidLength>19){
            $iccid = substr($iccid,0,strlen($iccid)-1);
        }
        $app_id='LA3PnZ7Ki1';
        $app_secret='DcgXQDQGTAM8ySaxV9EfveDesHdxu3';//生产
        $app_secret2='Y9KsXj7LHzB0yfHqx374vtvJG2DU0W';//沙箱
        $api_url =  'https://rnr.10646.cn'; //
        $url = "https://gwapi.10646.cn/api/realNameStatusQuery/V1/1Main/vV1.1";
        
        $timestamp=$this->msecdate($this->microtime());//毫秒级时间戳
        $trans_id=$this->msecdate2($this->microtime());

        $tokens = "app_id".$app_id."timestamp".$timestamp."trans_id".$trans_id.$app_secret;
        
        $sm3Url = 'http://shop.ly186.cn/sm3Api.php?parm='.$tokens;
        $sm3Result = json_decode($this->getUrl($sm3Url),True);
        
        $parm = array(
            'app_id' => $app_id,
            'timestamp' => $timestamp,
            'trans_id' => $trans_id,
            'token' => $sm3Result['data'],
            'data' => array(
                'iccid'=>$iccid
            ),
        );
        $data_string = json_encode($parm);
        echo $this->postUrl($url,$data_string);
    }
    /**
     * get curl
     * @param $url
     * @return mixed
     */
    public function getUrl($url){
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
        $tmpInfo = curl_exec($curl);     //返回api的json对象
        //关闭URL请求
        curl_close($curl);;
        return $tmpInfo;    //返回json对象
        //exit;
    }
    /**
     * post curl
     * @param $url
     * @return mixed
     */
    public function postUrl($url,$data_string){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 跳过证书检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);  // 从证书中检查SSL加密算法是否存在
        curl_setopt($ch, CURLOPT_REFERER, 'https://rnr.10646.cn/portal/fast/auth');//模拟来路

        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string)));
        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            print "错误：".curl_error($ch);
        }
        curl_close($ch);
        return $result;
    }

}